import {useMusicStore} from "../useMusicStore/index.js";
import {IconPauseFill, IconPlayArrowFill} from "../../icons/index.js";
import {useProgress} from "./useProgress.js";

export function useAudioPlayer() {
    const playlist = useMusicStore();
    const audio = document.createElement('audio');
    const progress = useProgress(audio, {
        setPlayState(state) {
            currentMusic.playState = state;
        }
    });
    let currentIndex = 0;
    let currentMusic = {
        music: {},
        playState: false, // false 停止播放 true 开始播放
        loopOne: false, // 是否单曲循环
        voiceValue: 100, // 当前音量
    };
    const elements = {};

    function setMusicLyrics(music) {
        let lyrics = music.lyrics.split('\n');
        elements.musicLyrics.innerHTML = lyrics.map(line => {
            const rightIndex = line.indexOf(']');
            const time = line.substring(1, rightIndex);
            if (time.match(/[a-zA-Z]/g) || !time) return false;
            const content = line.substring(rightIndex + 1, line.length);
            return `<div id="${time}" class="lyrics-line">${content}</div>`
        }).filter(Boolean).join('\n');

        const musicLyricsWrapper = document.querySelector('.main .music-lyrics-wrapper');

        musicLyricsWrapper.querySelectorAll('.lyrics-line').forEach(line => {
            line.addEventListener('click', () => {
                console.log('line click')
                progress.setCurrentTimeByID(line.id);
            });
        });
    }

    function setCurrentMusic(index = 0) {
        const music = playlist[index];
        if (!music) return;

        currentMusic.music = music;
        currentIndex = index;
        audio.src = music.play_url;
        elements.musicCover.src = music.img;
        elements.musicTitle.innerText = music.song_name;
        elements.musicAuthorName.innerText = music.author_name;
        setMusicLyrics(music);

        if (currentMusic.playState) {
            play();
        } else {
            pause();
        }
    }

    function toggle(playButton) {
        if (audio.paused) {
            play()
            playButton.innerHTML = IconPauseFill({size: 32})
        } else {
            pause();
            playButton.innerHTML = IconPlayArrowFill({size: 32})
        }
    }

    function play() {
        currentMusic.playState = true;
        audio.play();
    }

    function pause() {
        currentMusic.playState = false;
        audio.pause();
    }

    function prev() {
        setCurrentMusic(currentIndex - 1);
    }

    function next() {
        setCurrentMusic(currentIndex + 1);
    }

    function init() {
        elements.musicCover = document.querySelector('.main .music-info .music-cover img');
        elements.musicTitle = document.querySelector('.main .music-info .music-metadata .music-title');
        elements.musicAuthorName = document.querySelector('.main .music-info .music-metadata .music-author-name');
        elements.musicLyrics = document.querySelector('.main .music-lyrics');

        progress.init();
    }

    return {
        progress,
        playlist,
        currentMusic,
        setCurrentMusic,
        play,
        pause,
        toggle,
        prev,
        next,
        init,
    }
}

export const GlobalAudioPlayer = useAudioPlayer();
window.GlobalAudioPlayer = GlobalAudioPlayer;
